/**
 * Copyright (c) 2015-2016, Michael Yang 杨福海 (fuhai999@gmail.com).
 *
 * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.gnu.org/licenses/lgpl-3.0.txt
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.wzgl.core.model.query;

import java.util.LinkedList;
import java.util.List;

import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.ehcache.IDataLoader;
import com.wzgl.core.model.Tusers;

public class UserQuery extends JBaseQuery {
	protected static final Tusers DAO = new Tusers();
	private static final UserQuery QUERY = new UserQuery();

	public static UserQuery me() {
		return QUERY;
	}

	public List<Tusers> findList(int page, int pagesize, String status, String orderBy) {
		StringBuilder sqlBuilder = new StringBuilder("select * from user u ");
		LinkedList<Object> params = new LinkedList<Object>();

		boolean needWhere = true;
		needWhere = appendIfNotEmpty(sqlBuilder, "u.status", status, params, needWhere);

		buildOrderBy(orderBy, sqlBuilder);

		sqlBuilder.append(" LIMIT ?, ?");
		params.add(page - 1);
		params.add(pagesize);

		if (params.isEmpty()) {
			return DAO.find(sqlBuilder.toString());
		} else {
			return DAO.find(sqlBuilder.toString(), params.toArray());
		}

	}


	public Page<Tusers> paginate(int pageNumber, int pageSize , String orderby) {
		String select = "select * ";
		StringBuilder fromBuilder = new StringBuilder(" from tusers u ");
		buildOrderBy(orderby, fromBuilder);
		return DAO.paginate(pageNumber, pageSize, select, fromBuilder.toString());
	}

	public long findCount() {
		return DAO.doFindCount();
	}

	public Tusers findById(final String userId) {
		return DAO.getCache(userId, new IDataLoader() {
			@Override
			public Object load() {
				return DAO.findById(userId);
			}
		});
	}

	public Tusers findUserByEmail(final String email) {
		return DAO.getCache(email, new IDataLoader() {
			@Override
			public Object load() {
				return DAO.doFindFirst("faccount = ?", email);
			}
		});
	}

	public Tusers findUserByUsername(final String username) {
		return DAO.getCache(username, new IDataLoader() {
			@Override
			public Object load() {
				return DAO.doFindFirst("fname = ?", username);
			}
		});
	}

	public Tusers findUserByMobile(final String mobile) {
		return DAO.getCache(mobile, new IDataLoader() {
			@Override
			public Object load() {
				return DAO.doFindFirst("fmobile = ?", mobile);
			}
		});
	}

	protected static void buildOrderBy(String orderBy, StringBuilder fromBuilder) {
		if ("fname".equals(orderBy)) {
			fromBuilder.append(" ORDER BY u.fname DESC");
		}

		else if ("fnickname".equals(orderBy)) {
			fromBuilder.append(" ORDER BY u.fnickname DESC");
		}

		else if ("fstatus".equals(orderBy)) {
			fromBuilder.append(" ORDER BY u.fstatus DESC");
		}

		else if ("faccount".equals(orderBy)) {
			fromBuilder.append(" ORDER BY u.faccount DESC");
		}

		else if ("flevel".equals(orderBy)) {
			fromBuilder.append(" ORDER BY u.flevel DESC");
		}

		else if ("fctime".equals(orderBy)) {
			fromBuilder.append(" ORDER BY u.fctime DESC");
		}

		else if ("futime".equals(orderBy)) {
			fromBuilder.append(" ORDER BY u.futime DESC");
		}

		else {
			fromBuilder.append(" ORDER BY u.fctime DESC");
		}
	}

}
